package com.sgb.kjwl.view.ui.activity.manage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.androidkun.xtablayout.XTabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTheEnterpriseOfBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter5;
import com.sgb.kjwl.view.ui.fragment.manage.NoTypeFragment;
import com.sgb.kjwl.view.ui.fragment.manage.SupplierServiceProviderFragment;
import com.sgb.kjwl.view.widget.ActionBar;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.viewmodel.manage.TheEnterpriseOfModel;
import com.swgk.core.base.BaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 企业大全
 * CFK
 */
public class TheEnterpriseOfActivity extends AppActivity {
    @Inject
    TheEnterpriseOfModel model;
    private ActivityTheEnterpriseOfBinding binding;
    private ArrayList<BaseFragment> fragmentList;
    private String longitude;
    private String latitude;
    private boolean isMap;
    private IpPopWindow homeGoodsSelectionTypePopWindow;
    private String mIntroduction;
    private List<PickSelectEntity<List<PickSelectEntity>>> list;

    public static void start(Context context, String longitude, String latitude, boolean isMap) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("longitude", longitude);
        bundle.putString("latitude", latitude);
        bundle.putBoolean("isMap", isMap);
        intent.putExtras(bundle);
        intent.setClass(context, TheEnterpriseOfActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isMap = getIntent().getBooleanExtra("isMap", false);
        if (isMap) {
            mActionBar.setTitle("地图查找");
            mActionBar.setTitleVisible(false);
            mActionBar.setEditVisible(false);
            mActionBar.setMenuFunctionVisible(false);
        }

        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                int position = binding.tabLayout.getSelectedTabPosition();
                if (list.get(position).getChildren() != null) {
                    ((SupplierServiceProviderFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                } else {
                    ((NoTypeFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                }
            }
        });
        mActionBar.setClearEdiText(new ActionBar.ClearEdiText() {
            @Override
            public void clearEdiText() {
                int position = binding.tabLayout.getSelectedTabPosition();
                if (list.get(position).getChildren() != null) {
                    ((SupplierServiceProviderFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                } else {
                    ((NoTypeFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                }
            }

            @Override
            public void search() {
                closeKeybord();
                int position = binding.tabLayout.getSelectedTabPosition();
                if (list.get(position).getChildren() != null) {
                    ((SupplierServiceProviderFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                } else {
                    ((NoTypeFragment) fragmentList.get(position)).data(mActionBar.getEditText(), mIntroduction);
                }
            }
        });
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent)
                .enterpriseManageModule(new EnterpriseManageModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(TheEnterpriseOfActivity.this, R.layout.activity_the_enterprise_of);
        binding.setViewModel(new BaseViewModel());
        longitude = getIntent().getStringExtra("longitude");
        latitude = getIntent().getStringExtra("latitude");

        model.getTypeList(false);

        binding.editView.setOnClickListener(this);

        homeGoodsSelectionTypePopWindow = new IpPopWindow(this);
        homeGoodsSelectionTypePopWindow.setDefItem(0);
        homeGoodsSelectionTypePopWindow.setBgView(binding.grayLayout);

//        binding.viewpager.setOnTouchListener((v, event) -> true);
        homeGoodsSelectionTypePopWindow.setOnResultClick((key1, key2, key3) -> {
            binding.tabLayout.getTabAt(((int) key2)).select();
            binding.editView.setImageResource(R.mipmap.btn_expand1);
        });
        homeGoodsSelectionTypePopWindow.setOnDismissListener(() -> binding.editView.setImageResource(R.mipmap.btn_expand1));
    }

    public void initFragment(List<PickSelectEntity<List<PickSelectEntity>>> list, boolean isList) {
        this.list = list;
        if (!isList) {
            fragmentList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getChildren() != null) {
                    fragmentList.add(SupplierServiceProviderFragment.newInstance(list.get(i).getChildren(), longitude, latitude, isMap, list.get(i).getNo()));
                } else {
                    fragmentList.add(NoTypeFragment.newInstance(this, longitude, latitude, isMap, list.get(i).getNo()));
                }
            }
            CommPagerAdapter5 pagerAdapter = new CommPagerAdapter5(getSupportFragmentManager(), fragmentList, list);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.tabLayout.setupWithViewPager(binding.viewpager);
            binding.tabLayout.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(XTabLayout.Tab tab) {
                    mIntroduction = list.get(tab.getPosition()).getNo();
                }

                @Override
                public void onTabUnselected(XTabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(XTabLayout.Tab tab) {

                }
            });
        } else {
            List<IpPickSelectEntity> dictData = new ArrayList<>();
            for (PickSelectEntity oneDatum : list) {
                IpPickSelectEntity menuEntity = new IpPickSelectEntity(oneDatum.getNo(), oneDatum.getProjectscale(),"","","","","");
                dictData.add(menuEntity);
            }
            homeGoodsSelectionTypePopWindow.showPopwindow(binding.rlTop, 0,0);
            homeGoodsSelectionTypePopWindow.setData(dictData);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.edit_view:
                model.getTypeList(true);
                binding.editView.setImageResource(R.mipmap.btn_expand_up_gray);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1){
//            if (data.getBooleanExtra("关注",false)){
//                mapInfoDialog6.updateText();
//
//                entity.setFollow(!(entity.getFollow()));
//                mapList.get(Integer.parseInt(mMapTitle)).setFollow(!(mapList.get(Integer.parseInt(mMapTitle)).getFollow()));
//                resetMarker(markerLast.getId(), markerLast, mPosition);
//            }
        }
    }
}

